package sis.studentinfo;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-27 11:41
 * @notify
 * @version 1.0
 */

import com.jimbob.ach.*;
import sis.security.Accountable;

import java.math.BigDecimal;

public class Account implements Accountable {
    //收款金额
    private BigDecimal balance = new BigDecimal("0.00");

    //收款次数
    private int transactionCount = 0;

    //企业号
    private String bankAba;

    //账号
    private String bankAccountNumber;

    //交易类型
    private BankAccountType bankAccountType;

    private Ach ach;

    public enum BankAccountType {
        CHECKING("ck"), SAVINGS("sv");

        private String value;

        private BankAccountType(String value) {
            this.value = value;
        }

        public String toString() {
            return value;
        }
    }

    //提款
    public  void withdraw(BigDecimal amount) {
        //如果取款金额比当前余额大.则什么都不做
        synchronized(this){
            if (amount.compareTo(balance) > 0) {
                return;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {}
            //当前余额减去 取款金额
            balance = balance.subtract(amount);
        }
    }

    //这两个方法用来 创建两个所需参数
    private AchCredentials createCredentials() {
        AchCredentials credentials = new AchCredentials();
        credentials.merchantId = "12355";
        credentials.userName = "sismerc1920";
        credentials.password = "pitseleh411";
        return credentials;
    }

    //这两个方法用来 创建两个所需参数
    private AchTransactionData createData(BigDecimal amount) {
        AchTransactionData data = new AchTransactionData();
        data.description = "transfer from bank";
        data.amount = amount;
        data.aba = bankAba;
        data.account = bankAccountNumber;
        data.accountType = bankAccountType.toString();
        return data;
    }

    //获取 银行操作对象
    public Ach getAch() {
        return ach;
    }

    //设置 银行操作对象
    public void setAch(Ach ach) {
        this.ach = ach;
    }

    //增加收款金额!!!!  U
    public void credit(BigDecimal amount) {
        balance = balance.add(amount);
        transactionCount++;
    }


    //企业号!!!!  U
    public void setBankAba(String bankAba) {
        this.bankAba = bankAba;
    }

    //账号!!!!  U
    public void setBankAccountNumber(String bankAccountNumber) {
        this.bankAccountNumber = bankAccountNumber;
    }

    //设置交易类型!!!!  U
    public void setBankAccountType(Account.BankAccountType bankAccountType) {
        this.bankAccountType = bankAccountType;
    }

    //转账!!!! U
    public void transferFromBank(BigDecimal amount) {

        AchCredentials credentials = createCredentials();
        AchTransactionData data = createData(amount);
        Ach ach = getAch();

        AchResponse achResponse = ach.issueDebit(credentials, data);
        credit(amount);
    }

    //获取总金额!!!! R
    public BigDecimal getBalance() {
        return balance;
    }

    //平均收款!!!! R
    public BigDecimal transactionAverage() {
        return balance.divide(new BigDecimal(transactionCount), BigDecimal.ROUND_HALF_UP);
    }
}
